<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class At_link extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct()
	{
		parent::__construct();
		session_start();
	}

	public function index()
	{
		//$temp=$this->session->userdata('access_token');
		$temp=$_SESSION['token']['access_token'];
		$code=0;	//$code分两种情况：0-去不到链接（说明@错了人）；1-正常取到链接
		$name_link='';	//从API调回的链接
		$response=array();
		//var_dump($GLOBALS['weibos']);
			
		if ( empty($temp))   //如果用户没登录
		{
			$o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
			$code_url = $o->getAuthorizeURL(WB_CALLBACK_URL);
			echo "<meta http-equiv=refresh content='0; url=$code_url'>";  //跳转到授权页面
		}
		else
		{
			$c = new SaeTClientV2( WB_AKEY , WB_SKEY ,$temp );
			
			
			$name=$this->input->get("name");	//从AJAX的参数中获取name
			$user_info=$c->show_user_by_name( $name ); 	//掉去API获取该用户的信息
			
			if( ! empty($user_info))	//	如果正常调取到用户信息
			{
				$code=1;
				$name_link="http://www.weibo.com/u/".$user_info['idstr'];
				//echo "<meta http-equiv=refresh content='0; url=".$name_link."'>";
				//header('Location: http://www.example.com'); 
				//redirect('http://www.baidu.com');
			}
		}
		
		$response=array
		(
			'code'=>$code,
			'name_link'=>$name_link
		);
		
		echo json_encode($response);
	}
}
